package gw.com.android.ui.news;


import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import gw.com.android.presenter.http.CalendarRequest;
import gw.com.android.utils.ColorThemeUtil;

/**
 * 搜索适配器
 * Created by hubble on 2017/3/1.
 */
public class CalendarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int VIEW_TYPE_DATA = 1;
    private final static int VIEW_TYPE_EVENT = 2;
    private final static int VIEW_TYPE_VACATION = 3;

    private Context mContext;
    private List<CalendarRequest.CalendarData> mDataList;

    public CalendarAdapter(Context context) {
        this.mContext = context;
    }

    public void updateData(List<CalendarRequest.CalendarData> mDataList) {
        this.mDataList = mDataList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh;
        switch (viewType) {
            case VIEW_TYPE_DATA:
                vh = new DataViewHolder(View.inflate(mContext, R.layout.item_calendar_data, null));
                break;
            case VIEW_TYPE_EVENT:
                vh = new EventViewHolder(View.inflate(mContext, R.layout.item_calendar_event, null));
                break;
            case VIEW_TYPE_VACATION:
                vh = new VacationViewHolder(View.inflate(mContext, R.layout.item_calendar_vacation, null));
                break;
            default:
                vh = new DataViewHolder(View.inflate(mContext, R.layout.item_calendar_data, null));
                break;
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        CalendarRequest.CalendarData item = getItem(position);
        if (holder instanceof DataViewHolder) {
            bindDataItem((DataViewHolder) holder, item);
        } else if (holder instanceof EventViewHolder) {
            bindEventItem((EventViewHolder) holder, item);
        } else if (holder instanceof VacationViewHolder) {
            bindVacationItem((VacationViewHolder) holder, item);
        }
    }

    private void bindDataItem(DataViewHolder viewHolder, CalendarRequest.CalendarData data) {
        if (data == null) {
            return;
        }
        if (TextUtils.isEmpty(data.value)) {
            viewHolder.mRlItemBackground.setBackgroundColor(Color.TRANSPARENT);
        } else {
            viewHolder.mRlItemBackground.setBackgroundColor(0xFFFAFAFA);
        }
        if (data.time != null && data.time.length() >= 5) {
            String time = data.time.substring(0, 5);
            viewHolder.mTvTime.setText(time);
        } else {
            viewHolder.mTvTime.setText("00:00");
        }
        // 星级
        viewHolder.mRateBar.setRating(data.importanceLevel);
        // 描述
        parseProfitable(viewHolder.mTvDiscription, viewHolder.mTvPubValue, data.profitable);
        viewHolder.mTvPubValue.setTextColor(ColorThemeUtil.instance().IdleColor);
        // 内容
        //String content = data.country + "发布" + data.name;
        viewHolder.mTvMessage.setText(data.name);
        // 前值
        viewHolder.mTvLastValue.setText("".equals(data.lastValue) ? "--" : data.lastValue);
        // 预测
        viewHolder.mTvPreValue.setText("".equals(data.predictValue) ? "--" : data.predictValue);
        // 现值
        viewHolder.mTvPubValue.setText("".equals(data.value) ? "--" : data.value);
        //国旗
        viewHolder.mIvCountry.setImageResource(getIcFlagByName(data.country));
    }

    private void bindEventItem(EventViewHolder viewHolder, CalendarRequest.CalendarData data) {
        if (data.time != null && data.time.length() >= 5) {
            String time = data.time.substring(0, 5);
            viewHolder.mTvTime.setText(time);
        } else {
            viewHolder.mTvTime.setText("00:00");
        }
        // 星级
        viewHolder.mRateBar.setRating(data.importanceLevel);
        // 内容
        viewHolder.mTvMessage.setText(data.content);
        //国旗
        viewHolder.mIvCountry.setImageResource(getIcFlagByName(data.country));
    }

    private void bindVacationItem(VacationViewHolder viewHolder, CalendarRequest.CalendarData data) {
        // 内容
        String content = "";
        if (!TextUtils.isEmpty(data.title)) {
            content = data.title + ",";
        }
        content = content + data.content;
        viewHolder.mTvMessage.setText(content);
        //国旗
        viewHolder.mIvCountry.setImageResource(getIcFlagByName(data.country));
    }

    private void parseProfitable(TextView tvDescription, TextView tvPubValue, String profitable) {
        //String affect = "";
        if (!TextUtils.isEmpty(profitable)) {
            if (profitable.contains("利多")) {
                //affect = "利多金银";
                tvDescription.setTextColor(0xFFFD383F);
                tvPubValue.setTextColor(0xFFFD383F);
            } else if (profitable.contains("利空")) {
                //affect = "利空金银";
                tvDescription.setTextColor(0xFF2BBB72);
                tvPubValue.setTextColor(0xFF2BBB72);
            } else {
                //affect = "影响较小";
                tvDescription.setTextColor(0xFFADB2BB);
                tvPubValue.setTextColor(Color.BLACK);
            }
        }
        tvDescription.setText(profitable);
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    public CalendarRequest.CalendarData getItem(int position) {
        if (mDataList != null && mDataList.size() > position) {
            return mDataList.get(position);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < 0 || position >= getItemCount()) {
            return super.getItemViewType(position);
        }
        CalendarRequest.CalendarData data = getItem(position);
        if (data != null) {
            if (data.isFinanceData()) {
                return VIEW_TYPE_DATA;
            } else if (data.isFinanceEvent()) {
                return VIEW_TYPE_EVENT;
            } else if (data.isFinanceVacation()) {
                return VIEW_TYPE_VACATION;
            }
        }
        return super.getItemViewType(position);
    }

    //通过国家名获取国标id;
    private int getIcFlagByName(String currencyType) {
        if (TextUtils.isEmpty(currencyType)) {
            return R.mipmap.flag_national;
        }
        if (currencyType.contains("新西兰")) {
            return R.mipmap.flag_newzealand;
        } else if (currencyType.contains("韩国")) {
            return R.mipmap.flag_south_korea;
        } else if (currencyType.contains("澳大利亚")) {
            return R.mipmap.flag_australia;
        } else if (currencyType.contains("日本")) {
            return R.mipmap.flag_japan;
        } else if (currencyType.contains("德国")) {
            return R.mipmap.flag_germany; //EUR
        } else if (currencyType.contains("瑞士")) {
            return R.mipmap.flag_switzerland;
        } else if (currencyType.contains("香港")) {
            return R.mipmap.flag_china_hongkong;
        } else if (currencyType.contains("西班牙")) {
            return R.mipmap.flag_spain;
        } else if (currencyType.contains("英国")) {
            return R.mipmap.flag_greatbritain;
        } else if (currencyType.contains("意大利")) {
            return R.mipmap.flag_italy;
        } else if (currencyType.contains("加拿大")) {
            return R.mipmap.flag_canada;
        } else if (currencyType.contains("美国")) {
            return R.mipmap.flag_usa;
        } else if (currencyType.contains("中国")) {
            return R.mipmap.flag_china;
        } else if (currencyType.contains("台湾")) {
            return R.mipmap.flag_china_taiwan;
        } else if (currencyType.contains("印度")) {
            return R.mipmap.flag_india;
        } else if (currencyType.contains("法国")) {
            return R.mipmap.flag_france; //EUR
        } else if (currencyType.contains("欧洲") || currencyType.contains("欧元区")) {
            return R.mipmap.flag_euro;
        } else if (currencyType.contains("新加坡")) {
            return R.mipmap.flag_singapor;
        } else if (currencyType.contains("葡萄牙")) {
            return R.mipmap.flag_portugal;
        } else if (currencyType.contains("奥地利")) {
            return R.mipmap.flag_aodili;
        } else if (currencyType.contains("卢森堡")) {
            return R.mipmap.flag_luxembourg;
        } else if (currencyType.contains("荷兰")) {
            return R.mipmap.flag_netherlands;
        } else if (currencyType.contains("俄罗斯")) {
            return R.mipmap.flag_russia;
        } else {
            return R.mipmap.flag_national;
        }
    }

    public static class DataViewHolder extends RecyclerView.ViewHolder {
        private TextView mTvTime;
        private TextView mTvDiscription;
        private RatingBar mRateBar;
        private TextView mTvMessage;
        private TextView mTvLastValue;
        private TextView mTvPreValue;
        private TextView mTvPubValue;
        private ImageView mIvCountry;
        private RelativeLayout mRlItemBackground;

        DataViewHolder(final View itemView) {
            super(itemView);
            mTvTime = itemView.findViewById(R.id.tv_time);
            mTvDiscription = itemView.findViewById(R.id.tv_description);
            mRateBar = itemView.findViewById(R.id.rate13);
            mTvMessage = itemView.findViewById(R.id.tv_message);
            mTvLastValue = itemView.findViewById(R.id.tv_last_value);
            mTvPreValue = itemView.findViewById(R.id.tv_pre_value);
            mTvPubValue = itemView.findViewById(R.id.tv_pub_value);
            mRlItemBackground = itemView.findViewById(R.id.rl_item_back);
            mIvCountry = itemView.findViewById(R.id.iv_country);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //  do nothing 。。。。配合 recycler view 中的 onInterceptTouchEvent 拦截消费事件，处理首页中的滑动逻辑
                }
            });
        }
    }

    public static class EventViewHolder extends RecyclerView.ViewHolder {
        private TextView mTvTime;
        private RatingBar mRateBar;
        private TextView mTvMessage;
        private ImageView mIvCountry;

        EventViewHolder(final View itemView) {
            super(itemView);
            mTvTime = itemView.findViewById(R.id.tv_time);
            mRateBar = itemView.findViewById(R.id.rate13);
            mTvMessage = itemView.findViewById(R.id.tv_message);
            mIvCountry = itemView.findViewById(R.id.iv_country);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //  do nothing 。。。。配合 recycler view 中的 onInterceptTouchEvent 拦截消费事件，处理首页中的滑动逻辑
                }
            });
        }
    }

    public static class VacationViewHolder extends RecyclerView.ViewHolder {
        private TextView mTvMessage;
        private ImageView mIvCountry;

        VacationViewHolder(final View itemView) {
            super(itemView);
            //mTvTime = itemView.findViewById(R.id.tv_time);
            //mTvDiscription = itemView.findViewById(R.id.tv_description);
            //mRate = itemView.findViewById(R.id.rate);
            mTvMessage = itemView.findViewById(R.id.tv_message);
            mIvCountry = itemView.findViewById(R.id.iv_country);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //  do nothing 。。。。配合 recycler view 中的 onInterceptTouchEvent 拦截消费事件，处理首页中的滑动逻辑
                }
            });
        }
    }

}
